/*
 * Copyright (c) 2021-present, the hapjs-platform Project Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
import filter from './filter'
const prompt = require('@system.prompt')
const router = require('@system.router')

function queryString(url, query) {
  let str = []
  for (let key in query) {
    str.push(key + '=' + query[key])
  }
  let paramStr = str.join('&')
  return paramStr ? `${url}?${paramStr}` : url
}
function showToast(message = '', duration = 0) {
  if (!message) return
  prompt.showToast({
    message: message,
    duration
  })
}
function turnback() {
  router.back()
}
function formatDate(date, symbols = '-') {
  date = new Date(date)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  const monthDay = symbols == 'zh' ? m + '月' + d + '日' : m + symbols + d
  let w = date.getDay()
  let h = date.getHours()
  h = h < 10 ? '0' + h : h
  let min = date.getMinutes()
  min = min < 10 ? '0' + min : min
  const hoursMinutes = h + ':' + min
  const week = '周' + ['日', '一', '二', '三', '四', '五', '六'][w]
  const yearMonthDay =
    symbols == 'zh'
      ? y + '年' + m + '月' + d + '日'
      : y + symbols + m + symbols + d

  return {
    hoursMinutes,
    week,
    monthDay,
    yearMonthDay
  }
}
function isNumber(value) {
  return /^[0-9]$/.test(value);
} 
function calculateMinutes(t1, t2) {
  // 将时间戳转换为Date对象
  const date1 = Number(t1);
  const date2 = Number(t2);
  // 计算时间差（毫秒）
  const timeDifference = Math.abs(date1-date2);
  // 将毫秒转换为分钟
  const minutesDifference = Math.floor(timeDifference / (1000 * 60));
  // console.error(date1,date2,timeDifference,minutesDifference)
  return minutesDifference;
}
export default {
  ...filter,
  showToast,
  queryString,
  formatDate,
  turnback,
  isNumber,
  calculateMinutes
}
 